AddCSLuaFile( "cl_init.lua" )
AddCSLuaFile( "shared.lua" )
include('shared.lua')

function ENT:Initialize()
	self.Entity:SetModel( "models//props_phx//mk-82.mdl" )
	self.BaseClass.Initialize(self)
	
	self.Entity.speed = 600
	self.Entity.minspeed = 600
	self.Entity.maxspeed = 1800
	self.Entity.acceltime = 3
	
	self.Entity.buscount = 8
	
	local hackent = ents.Create("helper_point")
	hackent:SetPos(self.Entity:LocalToWorld(Vector(-60, 0, 10)))
	hackent:Spawn()
	hackent:SetParent(self.Entity)
	local trail = util.SpriteTrail(hackent, 0, self.Entity.Color, false, 10, 0, 3.2, 0.25, "trails/smoke.vmt")
end

function ENT:Think()
	if (self.Entity.Target != nil and self.Entity.Target:IsValid()) and (CDamage_Vector_Length_Compare((self.Entity:GetPos() - self.Entity.Target:GetPos()), 4500) <= 0) and self.Entity.Exists and (self.speed > (self.Entity.maxspeed / self.Entity.acceltime)) then
		self.Entity:Burst()
	end
	self.BaseClass.Think(self)
end

function ENT:Burst()
	local contraptiontarget = CDE_FindContraption( self.Entity.Target )
	for i=1,self.Entity.buscount do
		local shot = ents.Create("ordinance_missile_micromissile")
		shot:SetPos(self.Entity:LocalToWorld(self.Entity:OBBCenter()))
		shot:SetAngles((self.Entity:LocalToWorld(Vector(25, math.Rand(-1, 1), math.Rand(-1,1))) - self.Entity:GetPos()):Angle())
		shot:SetPlayer(self.Entity:GetPlayer())
		shot:SetOwner(self.Entity)
		shot.Target = contraptiontarget[math.random(1, #contraptiontarget)]
		shot.Damage = (self.Entity.Damage / self.Entity.buscount)
		shot.DamageType = self.DamageType
		shot.BlastRadius = self.BlastRadius
		shot:SetNetworkedEntity("parent", self.Entity)
		shot.Color = self.Color
		shot:Spawn()
		shot:Activate()
		shot:GetPhysicsObject():Wake()
		shot.minspeed = self.Entity.speed
	end
	self.Entity:DrawImpact(nil, Vector(0, 0, 0))
	self.Entity:Desolidify()
end

function ENT:DrawImpact(entity, normal)
	CDEffect_impact_solid(self.Entity:GetPos(), self.Entity.Color, 100, 1.2, normal, entity)
end

